package com.example.pinkcat.fragment.shouye;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;

import com.example.pinkcat.R;
import com.example.pinkcat.adapter.Zhuifan_m1_RecyclerViewAdapter;
import com.example.pinkcat.adapter.Zhuifan_m2_RecyclerViewAdapter;
import com.example.pinkcat.api.entity.myCollectStaticMsg;
import com.example.pinkcat.api.getData.getData_waibu;

public class Fragment_zhuifan extends Fragment implements SwipeRefreshLayout.OnRefreshListener{

    Context context;

    private Fragment_zhuifan fragment_zhuifan;
    private final int ONE_LINE_SHOW_NUMBER = 3;

    private RecyclerView zhuifanRV1;
    private RecyclerView zhuifanRV2;
    private SwipeRefreshLayout swipeRefreshLayout;
    public RelativeLayout zhuifan_rl;

    Zhuifan_m2_RecyclerViewAdapter zhuifanm2;
    Zhuifan_m1_RecyclerViewAdapter zhuifanm1;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_zhuifan,container,false);
    }

    @Override
    public void onResume() {
        super.onResume();


    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//        ViewGroup.LayoutParams layoutParams = zhuifanRV1.getLayoutParams();

        GridLayoutManager mLayoutManager2 = new GridLayoutManager(view.getContext(),2);
        mLayoutManager2.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(position==0){
                    return 2;
                }else if (position == 1&&!userStaticMsg.userId.equals("-1")){
                    return 2;}
                else {
                    return 1;
                }
            }
        });

        context=view.getContext();
        zhuifanRV2 = view.findViewById(R.id.zhuifan_RV_show_2);
        zhuifanRV2.setLayoutManager(mLayoutManager2);
        zhuifanm2 = new Zhuifan_m2_RecyclerViewAdapter(view.getContext());

        swipeRefreshLayout=(SwipeRefreshLayout) view.findViewById(R.id.zhuifan_srl);//初始
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.pink,R.color.purple_200);

        zhuifanRV2.setAdapter(zhuifanm2);

        getData_waibu.getZhuifanBanner(getContext(),zhuifanm2);
        getData_waibu.getZhuifanvideo(getContext(),zhuifanm2,"动漫");

    }
    //下拉刷新
    public void onRefresh(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("fragment","下拉刷新");
                int pageNumer = 0;;
                PostData(); //加载数据
                swipeRefreshLayout.setRefreshing(false);
            }
        },1000);
    }


    public void PostData(){
        getData_waibu.getZhuifanBanner(getContext(),zhuifanm2);
        getData_waibu.getZhuifanvideo(getContext(),zhuifanm2,"动漫");
    }
}
